package com.youyi.yyviewsdklibrary.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import com.lkx.liandianqinew.R;

public class MyColorTextView extends TextView {
    private OnColorTextListener mOnColorTextListener;
    private int mYyColor;
    private int mYyColorSize;
    private String mYyColorText;

    public interface OnColorTextListener {
        void onclick(View view);
    }

    public MyColorTextView(Context context) {
        super(context);
    }

    public MyColorTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.MyColorTextView);
        this.mYyColorText = obtainStyledAttributes.getString(R.styleable.MyColorTextView_yyColorText);
        this.mYyColor = obtainStyledAttributes.getColor(R.styleable.MyColorTextView_yyColor, -7829368);
        this.mYyColorSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.MyColorTextView_yyColorSize, sp2px(context, 13.0f));
        setData();
    }

    public void setOnColorTextListener(OnColorTextListener onColorTextListener) {
        this.mOnColorTextListener = onColorTextListener;
    }

    public void setYyColorText(String str) {
        this.mYyColorText = str;
        setData();
    }

    public void setYyColor(int i) {
        this.mYyColor = i;
        setData();
    }

    public void setYyColorSize(int i) {
        this.mYyColorSize = i;
        setData();
    }

    private void setData() {
        String charSequence = getText().toString();
        int indexOf = charSequence.indexOf(this.mYyColorText);
        if (indexOf != -1) {
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(charSequence);
            spannableStringBuilder.setSpan(new ForegroundColorSpan(this.mYyColor), indexOf, this.mYyColorText.length() + indexOf, 33);
            spannableStringBuilder.setSpan(new AbsoluteSizeSpan(this.mYyColorSize), indexOf, this.mYyColorText.length() + indexOf, 33);
            spannableStringBuilder.setSpan(new ClickableSpan() {
                /* class com.youyi.yyviewsdklibrary.View.MyColorTextView.AnonymousClass1 */

                public void onClick(View view) {
                    if (MyColorTextView.this.mOnColorTextListener != null) {
                        MyColorTextView.this.mOnColorTextListener.onclick(view);
                    }
                }
            }, indexOf, this.mYyColorText.length() + indexOf, 33);
            setText(spannableStringBuilder);
            setMovementMethod(LinkMovementMethod.getInstance());
        }
    }

    public int sp2px(Context context, float f) {
        return (int) ((f * context.getResources().getDisplayMetrics().scaledDensity) + 0.5f);
    }
}
